package com.css.dzj.zfjg.modules.fb.controller;

import com.css.aq.server.service.BaseService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.fb.entity.JgFbLhjgEntity;
import com.css.dzj.zfjg.modules.fb.service.JgFbLhjgService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * 联合监管任务分办表
 * 监管分办任务
 * @author liyuan
 */
@RestController
@RequestMapping("lhjg/jgfblhjg")
public class  JgFbLhjgController {
    @Autowired
    private JgFbLhjgService jgFbLhjgService;

    @Autowired
    private BaseService baseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgFbLhjgEntity> page = jgFbLhjgService.queryPage(pageRequest);
            baseService.logSuccess(3, "分页查询联合监管分办信息成功", "2");
            return R.ok().put("page", page);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "分页查询联合监管分办信息失败", "2");
            return R.error(e.getMessage());
        }
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{fbId}")
    public R info(@PathVariable("fbId") String fbId){
        try{
            JgFbLhjgEntity jgFbLhjg = jgFbLhjgService.getById(fbId);
            baseService.logSuccess(3, "通过id获取联合监管分办信息成功", "2");
            return R.ok().put("jgFbLhjg", jgFbLhjg);
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "通过id获取联合监管分办信息失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JgFbLhjgEntity jgFbLhjg){
        try{
            jgFbLhjgService.save(jgFbLhjg);
            baseService.logSuccess(3, "联合监管分办信息添加成功", "2");
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "联合监管分办信息添加失败", "2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody JgFbLhjgEntity jgFbLhjg){
        try {
            JgFbLhjgEntity oldjgFbLhjg = jgFbLhjgService.getById(jgFbLhjg.getFbId());
            BeanUtil.copyBeanNotEmptyToBean(jgFbLhjg, oldjgFbLhjg);
            jgFbLhjgService.update(jgFbLhjg);
            baseService.logSuccess(3, "联合监管分办信息修改成功", "2");
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            baseService.logFailed(3, "联合监管分办信息修改失败", "2");
            return R.error(e.getMessage());
        }
    }

}
